<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // unescape
      let MyLocal = {};
      let oneYear = new Date("2023-02-26").toUTCString();
      let lastYear = new Date("2021-02-26").toUTCString();
      console.log(oneYear);
      function getCookie() {
        return document.cookie;
      }
      function setCookie(value) {
        document.cookie = value;
      }
      MyLocal.setItem = function (key, value) {
        let cookie = getCookie();
        let str = `${key}=${escape(JSON.stringify(value))}`;
        setCookie([str, "expires=" + oneYear, "path=/"].join("; "));
      };
      MyLocal.getItem = function (key) {
        let cookieArr = getCookie().split("; ");
        for (let i = 0; i < cookieArr.length; i++) {
          let cookieKey = cookieArr[i].split("=")[0];
          let value = cookieArr[i].split("=")[1];
          console.log(key);
          if (cookieKey === key) {
            return JSON.parse(unescape(value));
          }
        }
        return -1;
      };
      MyLocal.removeItem = function (key) {
        console.log(key);
        let str = `${key}=`;
        document.cookie = [str, "expires=" + lastYear, "path=/"].join("; ");
      };
      MyLocal.clear = function (key) {
        let cookieArr = getCookie().split("; ");
        for (let i = 0; i < cookieArr.length; i++) {
          let cookieKey = cookieArr[i].split("=")[0];
          let value = cookieArr[i].split("=")[1];
          MyLocal.removeItem(cookieKey);
        }
      };
      MyLocal.clear();
    </script>
  </body>
</html>
